package ma.ensao.evalmetrics.view;
import java.util.List;

import org.hibernate.Session;
import java.util.Map;
import ma.ensao.evalmetrics.controller.AdminManager;
import ma.ensao.evalmetrics.model.Admin;
import ma.ensao.evalmetrics.util.HibernateUtil;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import java.util.ArrayList;
import java.util.Iterator;
import org.apache.struts2.interceptor.SessionAware;
import org.hibernate.HibernateException;
import org.hibernate.Query;
/*
* Authentification d'un Administrateur
*/
public class AdminAction extends ActionSupport implements SessionAware{

	private static final long serialVersionUID = 9149826260758390091L;
	private Admin admin;
	private List<Admin>  adminList;
	private AdminManager linkController;
	private Map<String, Object> session= ActionContext.getContext().getSession();
	public AdminAction() {
		linkController = new AdminManager();		
	}
	
	public String execute() {
		System.out.println("1"+getAdmin());
		//this.admin = (Admin) linkController.list();
		return SUCCESS;
	}
	public String Connect() {
		 String statut= null;
		 Long id = null;
		if (admin != null){
			System.out.println("adminAction"+ admin.getLogin()+admin.getPassword()+"\n");
			linkController.Connect(admin.getLogin(), admin.getPassword());
			System.out.println("hello statut="+linkController.getStatut()+linkController.getId_us());
			statut= linkController.getStatut();
			id= linkController.getId_us();
			if(statut != null && !statut.isEmpty()){
				statut= statut.toUpperCase();
				System.out.println(statut.toUpperCase());
				session.put("id", id);
				session.put("login", admin.getLogin());
				session.put("statut", linkController.getStatut());
				return statut;
			}else{
				System.out.println("null ");
	            addActionError(getText("error.login"));
				return "input";
			}
		}else{
			return "input";
		}
		
	}
	public String logOut() {
		System.out.println("idddd"+session.get("id"));
		System.out.println("login"+session.get("login"));
		session.remove("login");
		session.remove("id");

		return SUCCESS;
	}

	public List<Admin> getAdminList() {
		return adminList;
	}
	public void setAdmin(Admin admin) {
		this.admin = admin;
	}
	public Admin getAdmin() {
		return admin;
	}
	
	public void setAdminList(List<Admin> administrateurList) {
		this.adminList = administrateurList;
	}

	public Map<String, Object> getSession() {
		return session;
	}

	public void setSession(Map<String, Object> map) {
		this.session = map;
	}

	public AdminManager getLinkController() {
		return linkController;
	}

	public void setLinkController(AdminManager linkController) {
		this.linkController = linkController;
	}
}
